package com.leen.security;

import com.alibaba.fastjson.JSONObject;
import com.leen.admin.feign.IAdminFeignClient;
import com.leen.common.vo.ResultVO;
import com.leen.security.domain.AdminLoginUserDetails;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

/**
 * 类说明
 *
 * @author admin
 * @date 2021-05-19
 */
@Log4j2
@Component
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private IAdminFeignClient  iAdminFeignClient;


    @Override
    public UserDetails loadUserByUsername(String account) throws UsernameNotFoundException {

        //这里不能反序列化
        ResultVO<String> resultVO = iAdminFeignClient.findByAccount(account);

        //这里自己想办法解决
        if(resultVO.getCode()!=1){
           log.error(resultVO.getMessage());
            throw new UsernameNotFoundException(resultVO.getMessage());
        }

        String jsonStr= resultVO.getData();
        AdminLoginUserDetails adminLoginUserDetails = JSONObject.parseObject(jsonStr, AdminLoginUserDetails.class);

        return adminLoginUserDetails;
    }
}
